/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.patpro.controladores;

import com.patpro.modelo.javabeans.Curso;
import com.patpro.modelo.servicios.CursoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/** 

 */
public class ActualizaCursoController extends SimpleFormController {

    private CursoService servicio_curso;

    public ActualizaCursoController() {
        setCommandClass(Curso.class);
        setCommandName("cursoBean");
        //setSuccessView("exito");
        //setFormView("formView");
    }

    public CursoService getServicio_curso() {
        return servicio_curso;
    }

    public void setServicio_curso(CursoService servicio_curso) {
        this.servicio_curso = servicio_curso;
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        Curso c = (Curso) super.formBackingObject(request);
        String id = request.getParameter("id");
        if(id == null)
            c.setTipo(true);
        else{
            c = servicio_curso.buscarCurso(Integer.parseInt(id));
        }

        return c;
    }

    @Override
    public Map referenceData(HttpServletRequest request) {
        System.out.println(request.getRemoteUser());

        Map modelo = new HashMap();
        List<Curso> cursos = servicio_curso.listarCursos();
        modelo.put("lista_cursos", cursos);

        String id = request.getParameter("id");
        if(id == null)
            modelo.put("opcion", "Agregar");
        else
            modelo.put("opcion", "Actualizar");
        return modelo;
    }

    @Override
    protected ModelAndView onSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors) throws Exception {
        ModelAndView mv =null;

        Curso curso = (Curso) command;
        int resp = servicio_curso.actualizarCurso(curso);
        if(resp > 0)
            mv = new ModelAndView(getSuccessView());
        else
            mv = new ModelAndView("error");
        mv.addObject("controlador", "adminCurso");
        return mv;
    }
}
